/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2025-03-31     金鹏       the first version
 */

 #include "cmn_conver.h"

extern int cm_switch;

struct {
    unsigned int unicode;
    int value;
} hanzi_map[] = {
    {0x96F6, 0},  // 零（Unicode U+96F6）
    {0x4E00, 1},  // 一（U+4E00）
    {0x4E8C, 2},  // 二（U+4E8C）
    {0x4E09, 3},  // 三（U+4E09）
    {0x56DB, 4},  // 四（U+56DB）
    {0x4E94, 5},  // 五（U+4E94）
    {0x516D, 6},  // 六（U+516D）
    {0x4E03, 7},  // 七（U+4E03）
    {0x516B, 8},  // 八（U+516B）
    {0x4E5D, 9},  // 九（U+4E5D）
    {0x5341, 10}, // 十（U+5341）
};
/*
* hex_char_to_dec
* @Param name
*
* Change Logs:
* Date           Author        Notes
* 2025-03-31      金鹏       十六进制字符转十进制数值
*/
static int hex_char_to_dec(char c)
{
    if(c >= '0' && c <= '9'){
        return c - '0';
    }
    else if(c >= 'A' && c <= 'F'){
        return c - 'A' + 10;
    }
    else if(c >= 'a' && c <= 'f'){
        return c - 'a' + 10;
    }
}

/*
* utf8_to_unicode_mini
* @Param name
*
* Change Logs:
* Date           Author        Notes
* 2025-05-12      金鹏       UTF-8转unicode
*/
static unsigned int utf8_to_unicode_mini(const unsigned char *bytes) {
    if ((bytes[0] & 0xF0) != 0xE0) return 0xFFFFFFFF; // 非3字节汉字
    return ((bytes[0] & 0x0F) << 12) |
           ((bytes[1] & 0x3F) << 6) |
           (bytes[2] & 0x3F);
}



/*
* hex_to_bytes - 十六进制字符串转字节数组（自动剔除空格字符0x20）
* @param hex_str 输入十六进制字符串
* @param bytes   输出字节数组
*
* Change Logs:
* Date           Author        Notes
* 2025-03-31      金鹏       十六进制字符串转字节数组
* 2025-04-01      金鹏       新增空格剔除功能
*/
void hex_to_bytes(char *hex_str, unsigned char *bytes)
{
    int len = strlen(hex_str);
    int write_index = 0; // 独立写入指针

    for (int i = 0; i < len; i += 2) {
        // 边界检查（防止奇数长度越界）
        if (i+1 >= len) {
            bytes[write_index] = '\0';
            return;
        }

        // 字符有效性验证
        int high = hex_char_to_dec(hex_str[i]);
        int low = hex_char_to_dec(hex_str[i+1]);
        if (high == -1 || low == -1) {
            bytes[write_index] = '\0'; // 终止已转换部分
            return;
        }

        // 组合字节并过滤空格
        unsigned char byte = (high << 4) | low;
        if (byte != 0x20) { // 0x20是ASCII空格
            bytes[write_index++] = byte;
        }
    }

    bytes[write_index] = '\0'; // 正确设置终止符
}

/*
* utf8_to_unicode
* @Param name
*
* Change Logs:
* Date           Author        Notes
* 2025-03-31      金鹏                 utf8转unicode
*/
int utf8_to_unicode(unsigned char *str, unsigned char *out) {
    int i = 0;
    int count = 0;
    int u8_to_u16 = 0;
    if(*out == 'x'){
        u8_to_u16 = 1;
    }

    while (str[i] != '\0') {
        unsigned int unicode_char = 0;

        // 判断当前字节的前缀，确定字符的字节数
        if ((str[i] & 0x80) == 0) {
            // 1字节字符 (ASCII字符)
            unicode_char = str[i];
            i++;
        } else if ((str[i] & 0xE0) == 0xC0) {
            // 2字节字符
            unicode_char = (str[i] & 0x1F) << 6;
            unicode_char |= (str[i+1] & 0x3F);
            i += 2;
        } else if ((str[i] & 0xF0) == 0xE0) {
            // 3字节字符
            unicode_char = (str[i] & 0x0F) << 12;
            unicode_char |= (str[i+1] & 0x3F) << 6;
            unicode_char |= (str[i+2] & 0x3F);
            i += 3;
        } else if ((str[i] & 0xF8) == 0xF0) {
            // 4字节字符
            unicode_char = (str[i] & 0x07) << 18;
            unicode_char |= (str[i+1] & 0x3F) << 12;
            unicode_char |= (str[i+2] & 0x3F) << 6;
            unicode_char |= (str[i+3] & 0x3F);
            i += 4;
        }
        if(u8_to_u16 == 1){
            for(short j=0;j<11;j++){
                if(hanzi_map[j].unicode == unicode_char){
                    return j;
                }
            }
        }

        // 输出 Unicode 字符（16进制）
        *(out+(count++)) = (unicode_char & 0xFF00) >> 8;
        *(out+(count++)) = unicode_char & 0x00FF;
    }
    *(out+(count)) = '\0';

    return count;
}

/*
* hex_char_to_dec
* @Param name
*
* Change Logs:
* Date           Author        Notes
* 2025-04-22      金鹏                       切换网络 0:WIFI 1:4G
*/
void change_net()
{
    if(cm_switch == 1){
        cm_switch = 0;
    }
    else{
        cm_switch = 1;
    }

}

/*
* convert_hanzi_numbers
* @Param name
*
* Change Logs:
* Date           Author        Notes
* 2025-05-12      金鹏                汉字转数字(个位)
*/
int convert_hanzi_numbers(const unsigned char *str)
{
    short index = 0;
    unsigned char respon_unic[30] = "x";

    index = utf8_to_unicode(str,respon_unic);

    return index;
}

